package com.ss.mq.pattern.routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.ss.mq.util.RabbitUtils;

import java.util.Random;
import java.util.Scanner;

/**
 * @author 叁昇
 * @create 2020-12-13-21:52
 */
public class RoutingProducer {
    public static void main(String[] args) throws Exception {
        String[] levels = {"warning", "info", "error"};

        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();

        // 交换机类型: direct
        channel.exchangeDeclare("direct_log", BuiltinExchangeType.DIRECT);

        while (true) {
            System.out.print("请输入消息: ");
            String msg = new Scanner(System.in).nextLine();
            if ("exit".equals(msg)) break;

            // 模拟随机级别的日志
            String level = levels[new Random().nextInt(levels.length)];
            // 第二个参数是 routingKey, 使用设置好的日志级别作为路由键, 实现向不同队列发送特定消息
            channel.basicPublish("direct_log", level, null, msg.getBytes());
            System.out.println("消息已发送: [ "+ level +" - "+msg +" ]");
        }

        channel.close();
    }
}
